package com.coldworks.coldjoke.ui;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.coldworks.base.manager.BaseDisplayManager;
import com.coldworks.coldjoke.R;
import com.coldworks.coldjoke.model.JokeEntity;
import com.coldworks.coldjoke.task.VoteTask;
import com.coldworks.coldjoke.task.GetJokeReplyTask;
import com.coldworks.coldjoke.task.GetVoteTask;
import com.coldworks.coldjoke.task.JokeReplyTask;
import com.coldworks.coldjoke.ui.JokePageCell.JokePageCellDelegate;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class JokePageView extends RelativeLayout implements
		JokePageCellDelegate {
	protected final Logger log = LoggerFactory.getLogger(this.getClass());
	private Context context;
	private JokePageViewDelegate delegate;
	private ImageButton backBtn;
	private ImageButton shareBtn;
	private MenuShareJokeView mShareView;
	private Button commentBtn;
	private EditText commentContent;
	private LinearLayout operationBar;

	private JokeEntity entityPlaying;
	private JokePageCell cellLeft;
	private JokePageCell cellPlaying;
	private JokePageCell cellRight;

	public JokePageView(Context context) {
		super(context);
		setUpViews(context);
	}

	public JokePageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		setUpViews(context);
	}

	public JokePageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		setUpViews(context);
	}

	private void setUpViews(final Context context) {
		this.context = context;
		RelativeLayout view = (RelativeLayout) LayoutInflater.from(context)
				.inflate(R.layout.navigation_joke_page_view, this, false);
		cellLeft = (JokePageCell) view.findViewById(R.id.joke_page_cell_left);
		cellPlaying = (JokePageCell) view
				.findViewById(R.id.joke_page_cell_playing);
		cellRight = (JokePageCell) view.findViewById(R.id.joke_page_cell_right);
		backBtn = (ImageButton) view.findViewById(R.id.button_back);
		operationBar = (LinearLayout) view
				.findViewById(R.id.operation_bar_comment);
		commentContent = (EditText) view.findViewById(R.id.comment_content);
		commentBtn = (Button) view.findViewById(R.id.button_comment);
		shareBtn = (ImageButton) view.findViewById(R.id.button_share);
		mShareView = new MenuShareJokeView(context);

		backBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (delegate != null) {
					delegate.JPVshowList(entityPlaying);
				}
			}
		});
		commentBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (userState != null) {
					userState.JPVUScomment();
				}
			}
		});
		shareBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				JokePageView.this.share();
			}
		});
		addView(view);
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		return super.onInterceptTouchEvent(ev);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return true;
	}

	public void start(JokePageViewDelegate pDelegate, JokeEntity pEntityPlaying) {
		this.delegate = pDelegate;
		this.reload(pEntityPlaying);
	}

	/**
	 * 评论，需要登陆，供Fragment调用
	 */
	public void comment() {
		String text = commentContent.getText().toString();
		if (text.length() == 0) {
			Toast.makeText(context, "随便说点什么吧！", Toast.LENGTH_SHORT).show();
			return;
		}
		if (text.trim().length() == 0) {
			Toast.makeText(context, "元芳，空格键你怎么看！", Toast.LENGTH_SHORT).show();
			return;
		}
		String replyContent = text.trim();
		InputMethodManager imm = (InputMethodManager) context
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(commentContent.getWindowToken(),
				InputMethodManager.HIDE_NOT_ALWAYS);
		commentContent.setText("");
		new JokeReplyTask(context, replyContent, cellPlaying).execute();
	}

	/**
	 * 分享，不要求登陆
	 */
	private void share() {
		mShareView.show(backBtn, cellPlaying.getJokeModel(), null);
	}

	/**
	 * 收藏，需要登陆，供Fragment调用
	 */
	public void favorite() {
		Toast.makeText(context, "收藏，需要登陆", Toast.LENGTH_SHORT).show();
	}

	@Override
	public void JPCleft() {
		// log.info("left");
		if (entityPlaying.manager.getListShow().size() == 0) {
			return;
		}
		if (entityPlaying.location == entityPlaying.manager.getListShow()
				.size() - 1) {
			Toast.makeText(context, "已经是最后一篇", Toast.LENGTH_SHORT).show();
			return;
		}
		delegate.JPVselect(entityPlaying);
		final TranslateAnimation playingTrans = new TranslateAnimation(0.0f,
				-BaseDisplayManager.getInstance().getDisplayWidth(context),
				0.0f, 0.0f);
		playingTrans.setDuration(600);
		playingTrans.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
				cellLeft.setBgVisible(true);
				cellLeft.setVisibility(View.VISIBLE);
				entityPlaying.location++;
				cellPlaying.setJokeModel(
						JokePageView.this,
						entityPlaying,
						entityPlaying.manager.getListShow().get(
								entityPlaying.location));
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				cellLeft.setVisibility(View.GONE);
				cellLeft.setBgVisible(false);
				cellLeft.setJokeModel(
						JokePageView.this,
						entityPlaying,
						entityPlaying.manager.getListShow().get(
								entityPlaying.location));
				cellRight.setJokeModel(
						JokePageView.this,
						entityPlaying,
						entityPlaying.manager.getListShow().get(
								entityPlaying.location));
				new GetJokeReplyTask(context, cellPlaying).execute();
				new GetVoteTask(context, cellPlaying, entityPlaying.manager
						.getJokesSort()).execute();
			}
		});
		cellLeft.setAnimation(playingTrans);
		playingTrans.start();
	}

	@Override
	public void JPCright() {
		// log.info("right");
		if (entityPlaying.manager.getListShow().size() == 0) {
			return;
		}
		if (entityPlaying.location == 0) {
			Toast.makeText(context, "已经是最新一篇", Toast.LENGTH_SHORT).show();
			return;
		}
		delegate.JPVselect(entityPlaying);
		final TranslateAnimation playingTrans = new TranslateAnimation(
				-BaseDisplayManager.getInstance().getDisplayWidth(context),
				0.0f, 0.0f, 0.0f);
		playingTrans.setDuration(600);
		playingTrans.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
				cellRight.setVisibility(View.VISIBLE);
				cellPlaying.setVisibility(View.GONE);
				cellLeft.setBgVisible(true);
				cellLeft.setVisibility(View.VISIBLE);
				entityPlaying.location--;
				cellPlaying.setJokeModel(
						JokePageView.this,
						entityPlaying,
						entityPlaying.manager.getListShow().get(
								entityPlaying.location));
				cellLeft.setJokeModel(
						JokePageView.this,
						entityPlaying,
						entityPlaying.manager.getListShow().get(
								entityPlaying.location));
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				cellRight.setVisibility(View.GONE);
				cellPlaying.setVisibility(View.VISIBLE);
				cellLeft.setVisibility(View.GONE);
				// cellLeft.setBgVisible(false);
				cellRight.setJokeModel(
						JokePageView.this,
						entityPlaying,
						entityPlaying.manager.getListShow().get(
								entityPlaying.location));
				new GetJokeReplyTask(context, cellPlaying).execute();
				new GetVoteTask(context, cellPlaying, entityPlaying.manager
						.getJokesSort()).execute();
			}
		});
		cellLeft.setAnimation(playingTrans);
		playingTrans.start();
	}

	@Override
	public void JPClogin() {
		if (delegate != null) {
			delegate.JPVlogin();
		}
	}

	/**
	 * 重刷，每次从快速浏览到回到正常浏览模式时都必须调用一次，供Fragment调用
	 */
	public void reload(JokeEntity pEntityPlaying) {
		log.info("JPV reload type:" + pEntityPlaying.manager.getJokesSort()
				+ " size:" + pEntityPlaying.manager.getListShow().size()
				+ " location:" + pEntityPlaying.location);
		if (entityPlaying != pEntityPlaying) {
			entityPlaying = pEntityPlaying;
		}
		if (entityPlaying.manager.getListShow().size() == 0) {
			cellPlaying.setVisibility(View.GONE);
			cellLeft.setJokeModel(this, null, null);
			cellPlaying.setJokeModel(this, null, null);
			cellRight.setJokeModel(this, null, null);
			return;
		}
		cellPlaying.setVisibility(View.VISIBLE);
		cellPlaying.setJokeModel(this, entityPlaying, entityPlaying.manager
				.getListShow().get(entityPlaying.location));
		cellLeft.setJokeModel(this, entityPlaying, entityPlaying.manager
				.getListShow().get(entityPlaying.location));
		cellRight.setJokeModel(this, entityPlaying, entityPlaying.manager
				.getListShow().get(entityPlaying.location));
		new GetJokeReplyTask(context, cellPlaying).execute();
		new GetVoteTask(context, cellPlaying,
				entityPlaying.manager.getJokesSort()).execute();
	}

	public void doneNew(JokeEntity jokeEntity, int newCount) {
		log.info("JPV doneNew type:" + jokeEntity.manager.getJokesSort() + " "
				+ newCount);
		if (entityPlaying == jokeEntity
				&& (newCount > 0 || cellPlaying.getJokeModel() == null)) {
			jokeEntity.location = 0;
			this.reload(jokeEntity);
		}
	}

	public void doneNext(JokeEntity jokeEntity, int nextCount) {
		log.info("JPV doneNext type:" + jokeEntity.manager.getJokesSort() + " "
				+ nextCount);
		if (entityPlaying == jokeEntity
				&& jokeEntity.manager.getListShow().size() > 0
				&& cellPlaying.getJokeModel() == null) {
			jokeEntity.location = 0;
			this.reload(jokeEntity);
		}
	}

	public void doneNewAndNext(JokeEntity jokeEntity, int newCount,
			int nextCount) {
		log.info("JPV doneNewAndNext type:" + jokeEntity.manager.getJokesSort()
				+ " " + newCount + "+" + nextCount);
		if (entityPlaying == jokeEntity
				&& (newCount > 0 || cellPlaying.getJokeModel() == null)) {
			jokeEntity.location = 0;
			this.reload(jokeEntity);
			return;
		}
		if (entityPlaying == jokeEntity
				&& jokeEntity.manager.getListShow().size() > 0
				&& cellPlaying.getJokeModel() == null) {
			jokeEntity.location = 0;
			this.reload(jokeEntity);
		}
	}

	public void zoomIn() {
		final ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.5f, 1,
				0.5f, Animation.RELATIVE_TO_SELF, 0.5f,
				Animation.RELATIVE_TO_SELF, 0.5f);
		scaleAnimation.setDuration(500);
		scaleAnimation.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
				// JokePageView.this.setVisibility(View.VISIBLE);
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				JokePageView.this.setVisibility(View.GONE);
			}
		});
		final TranslateAnimation translateAnimation = new TranslateAnimation(0,
				0, 0, -300);
		translateAnimation.setDuration(500);
		AnimationSet set = new AnimationSet(true);
		set.addAnimation(translateAnimation);
		set.addAnimation(scaleAnimation);
		startAnimation(set);
	}

	public void zoomOut() {
		final ScaleAnimation scaleAnimation = new ScaleAnimation(0.5f, 1.0f,
				0.5f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f,
				Animation.RELATIVE_TO_SELF, 0.5f);
		scaleAnimation.setDuration(400);
		scaleAnimation.setStartOffset(200);
		scaleAnimation.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
				// setVisibility(View.GONE);
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				setVisibility(View.VISIBLE);
			}
		});
		final TranslateAnimation translateAnimation = new TranslateAnimation(0,
				0, -300, 0);
		translateAnimation.setDuration(400);
		translateAnimation.setStartOffset(200);
		AnimationSet set = new AnimationSet(true);
		set.addAnimation(translateAnimation);
		set.addAnimation(scaleAnimation);
		startAnimation(set);
	}

	public void hideBar() {
		operationBar.setVisibility(LinearLayout.GONE);
	}

	public void showBar() {
		operationBar.setVisibility(LinearLayout.VISIBLE);
	}

	/**
	 * 向Fragment发送事件请求，不要求注册,都是对界面的操作
	 * 
	 */
	public interface JokePageViewDelegate {

		/**
		 * 通知Fragment选中了响应笑话的位置
		 */
		void JPVselect(JokeEntity jokeEntity);

		/**
		 * 通知Fragment用户准备快速浏览
		 */
		void JPVshowList(JokeEntity jokeEntity);

		/**
		 * 通知Fragment展示登录提示窗口
		 */
		void JPVlogin();

	}

	public void setUserState(JPVUserStateBase newState) {
		userState = newState;
		if (userState == userStateLogin) {
			cellLeft.setUserState(cellLeft.userStateLogin);
			cellPlaying.setUserState(cellPlaying.userStateLogin);
			cellRight.setUserState(cellRight.userStateLogin);
		} else if (userState == userStateLogout) {
			cellLeft.setUserState(cellLeft.userStateLogout);
			cellPlaying.setUserState(cellPlaying.userStateLogout);
			cellRight.setUserState(cellRight.userStateLogout);
		}
	}

	private JPVUserStateBase userState;
	public JPVUserStateLogin userStateLogin = new JPVUserStateLogin();
	public JPVUserStateLogout userStateLogout = new JPVUserStateLogout();

	/**
	 * 向Fragment发送事件请求，要求注册,都是对内容的操作
	 * 
	 */
	public abstract class JPVUserStateBase {
		public abstract void JPVUScomment();

		public void JPVUSfavorite() {

		}

		public void JPVUScell() {

		}
	}

	public class JPVUserStateLogin extends JPVUserStateBase {
		public void JPVUScomment() {
			comment();
		}
	}

	public class JPVUserStateLogout extends JPVUserStateBase {
		public void JPVUScomment() {
			if (delegate != null) {
				delegate.JPVlogin();
			}
		}
	}

	@Override
	public void JPCvote(String itemType) {
		new VoteTask(context, cellPlaying,
				entityPlaying.manager.getJokesSort(), itemType).start();
	}

}
